<?php
namespace M;
use I\Singleton;
use I\DB;

class Middle_Feishu extends Singleton {

    public function send($content, $url ) {
        return;
        $data = [
            'msgtype' => 'markdown',
            'markdown' => ['text' => $content],
        ];

        $j_data = json_encode($data);

        //这里要填写对应webhook地址
        // 
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $j_data);

        // 设置执行超时（单位：秒）
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        
        $ret = curl_exec($curl);
        curl_close($curl);
        return $ret;
    }

    public function newtask($task) {
        return;
        $db = DB::write();
        $leader = $db->row("select * from users where id = {$task->leader}");
        $tag = $db->row("select * from tags where id = {$task->tag}");

        $at = "<at email=\"{$leader->feishu}\"></at>";

        $content = "{$at} 您一个有新任务等待接收\n\n";
        if ($task->priority == 999) {
            $content .= "<font color='red'>此任务优先级为加急</font>\n\n";
        }
        $content .= "# [#{$task->id} &{$tag->name}& {$task->title}](http://10.77.34.52/task/show/{$task->id})";

        //这里要填写对应webhook地址
        $url = '';
        $this->send($content, $url);
    }
}
